﻿package mortal.game.view.effect
{
    import com.gengine.global.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.game.manager.*;

    public class UIMaskEffect extends Sprite
    {
        private var _masks:Array;
        private var _layer:DisplayObjectContainer;
        private var _timeKey:uint;
        private var _maskAlpha:Number = 0.3;
        private var _clickBack:Function;

        public function UIMaskEffect()
        {
            this._layer = LayerManager.guideLayer;
            this.addEventListener(MouseEvent.CLICK, this.onMouseClickHandler);
            return;
        }// end function

        private function onMouseClickHandler(event:MouseEvent) : void
        {
            if (this._clickBack != null)
            {
                this._clickBack();
            }
            return;
        }// end function

        private function drawMask() : void
        {
            var _loc_1:MaskInfo = null;
            graphics.clear();
            graphics.lineStyle(0, 0, 0);
            graphics.beginFill(0, this._maskAlpha);
            graphics.drawRect(0, 0, Global.stage.stageWidth, Global.stage.stageHeight);
            if (this._masks)
            {
                for each (_loc_1 in this._masks)
                {
                    
                    switch(_loc_1.shape)
                    {
                        case MaskInfo.Shape_Cycle:
                        {
                            graphics.drawCircle(_loc_1.x, _loc_1.y, _loc_1.width);
                            break;
                        }
                        case MaskInfo.Shape_Rect:
                        {
                            graphics.drawRect(_loc_1.x, _loc_1.y, _loc_1.width, _loc_1.height);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
            }
            graphics.endFill();
            return;
        }// end function

        private function onTimeOutHandler() : void
        {
            this.hideMask();
            return;
        }// end function

        public function showMask(param1:Array, param2:int = 10000, param3:Number = 0.3, param4:Function = null) : void
        {
            this.hideMask();
            this._clickBack = param4;
            this._masks = param1;
            this._maskAlpha = param3;
            this.drawMask();
            this._layer.addChild(this);
            if (param2 > 0)
            {
                this._timeKey = setTimeout(this.onTimeOutHandler, param2);
            }
            return;
        }// end function

        public function hideMask() : void
        {
            this._clickBack = null;
            clearTimeout(this._timeKey);
            graphics.clear();
            if (this._masks)
            {
                this._masks.length = 0;
            }
            if (this._layer.contains(this))
            {
                this._layer.removeChild(this);
            }
            return;
        }// end function

        public function get isHide() : Boolean
        {
            return this.parent == null;
        }// end function

        public function stageResize() : void
        {
            if (this.parent)
            {
                this.drawMask();
            }
            return;
        }// end function

    }
}
